package com.mdp.tpa.pub.service;

import com.mdp.core.api.Sequence;
import com.mdp.core.service.SequenceService;
import com.mdp.core.utils.BaseUtils;
import com.mdp.tpa.pub.entity.SysUserTpaInvite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 用于传递给微信/微信回传的state的相关操作
 *
 */
@Service("mdp.tpa.pub.InviteCacheService")
public class InviteCacheService {

    @Autowired
    RedisTemplate redisTemplate;

    Sequence sequence=new SequenceService();

    public String createInviteId(String prefix){
       String seq= prefix+sequence.getCommonNo("{date62:yyyyMMddHHmmssS}{rands:8}");
       return seq;
    }
    public void setInvite(String inviteId, SysUserTpaInvite invite){

        redisTemplate.opsForHash().put(getCacheKey(),inviteId,BaseUtils.toMap(invite));
    }

    public boolean valiInviteId(String inviteId){
        return redisTemplate.opsForHash().hasKey(getCacheKey(),inviteId);
    }

    public void clearInviteId(String inviteId){
        redisTemplate.opsForHash().delete(getCacheKey(),inviteId);
    }
    public SysUserTpaInvite getInvite(String inviteId){
        Map<String,Object> map= (Map<String, Object>) redisTemplate.opsForHash().get(getCacheKey(),inviteId);
        if(map==null){
            return null;
        }else{
            return BaseUtils.fromMap(map,SysUserTpaInvite.class);
        }

    }

     public String getCacheKey() {
        return "mdp-invite";
    }
}
